/**
 * $Id: UserService.java May 30, 2015 10:05:19 AM hdp
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aurora.base.service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.aurora.app.dao.UserAccountHistoryDao;
import com.aurora.app.entity.UserAccountHistory;
import com.aurora.base.dao.BaseDao;
import com.aurora.base.dto.Order;
import com.aurora.base.dto.QueryResult;
import com.aurora.base.util.Utf8Util;

import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 
 * 
 * @since 2016-6-29
 * @author 林佳新 TODO
 */

@Service
public class UserAccountHistoryService extends BaseService<UserAccountHistory> {

	@Resource
	private UserAccountHistoryDao baseDao;

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.tentcoo.base.service.impl.AbstractBaseServiceImpl#getBaseDao()
	 */
	@Override
	public BaseDao<UserAccountHistory> getBaseDao() {
		return this.baseDao;
	}

	public List<UserAccountHistory> getListByPages(HttpServletRequest request) {
		List<Object> list = new ArrayList<>();
		Order order = new Order();
		order.put("createDate", order.DESC);
		// 筛选
		String nickname = Utf8Util.setUtf8(request.getParameter("nickname"));
		String action = Utf8Util.setUtf8(request.getParameter("action"));
		StringBuilder sb = new StringBuilder();

		if (StringUtils.isNotBlank(nickname)) {
			sb.append("and o.weixinUserAccount.user.nickname like ? ");
			list.add("%"+nickname+"%");
			request.setAttribute("nickname", nickname);
		}
		if (StringUtils.isNotBlank(action)) {
			sb.append("and o.action =  "+action);
			//list.add(Integer.valueOf(action));
			request.setAttribute("action", action);
		}
		String whereSql = "";
		if (sb.length() > 0) {
			whereSql = sb.toString();
			whereSql = whereSql.substring(3, whereSql.length());
		}
		Object[] params = list.toArray(new Object[list.size()]);
		QueryResult<UserAccountHistory> res = this.baseDao.query(request, order, whereSql, params);
		request.setAttribute("count", res.getCount());
		return res.getResults();
	}

}
